﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.InvDoc.CheckDiffBill;
    using UFIDA.U9.InvDoc.Enums;
    using UFIDA.U9.ISV.CheckDiffSV;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Util.Context;

    /// <summary>
    /// CheckDiffRevocateSV partial 
    /// </summary>	
    public partial class CheckDiffRevocateSV
    {
        internal BaseStrategy Select()
        {
            return new CheckDiffRevocateSVImpementStrategy();
        }
    }

    #region  implement strategy	
    /// <summary>
    /// Impement Implement
    /// 
    /// </summary>	
	internal partial class CheckDiffRevocateSVImpementStrategy : BaseStrategy
	{
		public CheckDiffRevocateSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			CheckDiffRevocateSV bpObj = (CheckDiffRevocateSV)obj;

            try
            {
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

                foreach (var dto in bpObj.Data)
                {
                    CheckDiffBill item = CheckDiffBill.Finder.Find($"DocNo='{dto.DocNo}'");
                    if (item == null)
                    {
                        //新增逻辑
                        throw new Exception($"单号:{dto.DocNo}不存在!");
                    }
                    else
                    {
                        //修改逻辑
                        if (item.Status != INVDocStatus.Approving)
                            throw new Exception($"单号[{item.DocNo}]状态不为审核中");

                        CheckDiffOperationSV sv = new CheckDiffOperationSV();
                        sv.CheckDiffList = new List<CheckDiffBill.EntityKey>()
                        {
                            item.Key
                        };
                        sv.Status = INVDocStatus.Open;

                        CommonArchiveDataDTOData ret = new CommonArchiveDataDTOData()
                        {
                            ID = item.ID,
                            Code = item.DocNo,
                        };
                        retlist.Add(ret);
                    }
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
            catch (Exception ex)
            {
                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
        }		
	}

	#endregion
	
	
}